<?php
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;
global $max_planets_sector;
global $default_prod_organics, $default_prod_ore, $default_prod_goods;
global $default_prod_energy, $default_prod_fighters, $default_prod_torp;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_gns_title']);

//-------------------------------------------------------------------------------------------------

//Add db lock to prevent more than 5 planets in a sector - rjordan

$lock = $smcFunc['db_query']('', '
	LOCK TABLES
	{raw:db_ships} WRITE,
	{raw:db_planets} WRITE,
	{raw:db_universe} READ,
	{raw:db_zones} READ',
	array(
		'db_ships' => $dbtables['ships'],
		'db_planets' => $dbtables['planets'],
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones']
	)
);

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Pull sector info into $sectorinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$result = $smcFunc['db_query']('', '
	SELECT planet_id
	FROM {raw:db_planets}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_planets' => $dbtables['planets'],
		'sector_id' => $playerinfo['sector']
	)
);
$num_planets = $smcFunc['db_num_rows']($result);
$smcFunc['db_free_result']($result);

// Generate Planetname
$planetname = substr($playerinfo['character_name'],0,1) . substr($playerinfo['ship_name'],0,1) . "-" . $playerinfo['sector'] . "-" . ($num_planets + 1);

if ($playerinfo['turns'] < 1)
	echo $txt['bnt_gns_turn'];
elseif ($playerinfo['on_planet']=='Y')
	echo $txt['bnt_gns_onplanet'];
elseif ($num_planets >= $max_planets_sector)
	echo $txt['bnt_gns_full'];

/* -------------------------------------------------------------- *
 * I'm (SharpBlue) lazy. With many planets by sector that code    *
 * becomes pretty hard to manage, and besides... Isn't it a       *
 * little too powerful?                                           *
 * -------------------------------------------------------------- *
elseif($sectorinfo[planet] == "Y")
{
  echo "There is already a planet in this sector.";
  if($playerinfo[ship_id]==$sectorinfo[planet_owner])
  {
    if($destroy==1 && $allow_genesis_destroy)
    {
    // not multilingualed cause its not working right now anyway
      echo "<BR>Are you sure???<BR><A HREF=genesis.php?destroy=2>YES, Let them die!</A><BR>";
      echo "<A HREF=device.php>No! That would be Evil!</A><BR>";
    }
    elseif($destroy==2 && $allow_genesis_destroy)
    {
      if($playerinfo[dev_genesis] > 0)
      {
        $deltarating=$sectorinfo[planet_colonists];
        $update = $db->Execute("UPDATE $dbtables[universe] SET planet_name=NULL, planet_organics=0, planet_energy=0, planet_ore=0, planet_goods=0, planet_colonists=0, planet_credits=0, planet_fighters=0, planet_owner=null, planet_corp=null, base='N',base_sells='N', base_torp=0, planet_defeated='N', planet='N' WHERE sector_id=$playerinfo[sector]");
        $update2=$db->Execute("UPDATE $dbtables[ships] SET turns_used=turns_used+1, turns=turns-1, dev_genesis=dev_genesis-1, rating=rating-$deltarating WHERE ship_id=$playerinfo[ship_id]");
        echo "<BR>Errr, there was one with $deltarating colonists here....<BR>";
      }
      else
      {
        echo "$l_gns_nogenesis";
      }
    }
    elseif($allow_genesis_destroy)
    {
      echo "<BR>Do you want to destroy <A HREF=genesis.php?destroy=1>";
      if($sectorinfo[planet_name]=="")
      {
        echo "Unnamed</A>?";
      }
      else
      {
        echo $sectorinfo[planet_name] . "</A>?";
      }
    }
  }
}
* --------------------------------------------------------------- *
* If anyone who's coded this thing is willing to update it to     *
* support multiple planets, go ahead. I suggest removing this     *
* code completely from here and putting it in the planet menu     *
* instead. Easier to manage, makes more sense too.                *
* End of comments section.                                        *
* -------------------------------------------------------------- */

elseif ($playerinfo['dev_genesis'] < 1)
	echo $txt['bnt_gns_nogenesis'];
else
{
	$result = $smcFunc['db_query']('', '
		SELECT allow_planet, corp_zone, owner
		FROM {raw:db_zones}
		WHERE zone_id = {int:zone_id}',
		array(
			'db_zones' => $dbtables['zones'],
			'zone_id' => $sectorinfo['zone_id']
		)
	);
	$zoneinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	if ($zoneinfo['allow_planet'] == 'N')
		echo $txt['bnt_gns_forbid'];
	elseif ($zoneinfo['allow_planet'] == 'L')
	{
	    if ($zoneinfo['corp_zone'] == 'N')
	    {
			if ($playerinfo['team'] == 0 && $zoneinfo['owner'] <> $playerinfo['ship_id'])
				echo $txt['bnt_gns_forbid'];
			else
			{
				$result = $smcFunc['db_query']('', '
					SELECT team
					FROM {raw:db_ships}
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $zoneinfo['ship_id']
					)
				);
				$ownerinfo = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
				
				if	($ownerinfo['team'] != $playerinfo['team'])
					echo $txt['bnt_gns_forbid'];
				else
				{
					$smcFunc['db_insert']('insert',
						$dbtables['planets'],
						array(
							'sector_id' => 'int',
							'name' => 'string',
							'organics' => 'int', 
							'ore' => 'int', 
							'goods' => 'int',
							'energy' => 'int',
							'colonists' => 'int',
							'credits' => 'int',
							'fighters' => 'int',
							'torps' => 'int',
							'owner' => 'int',
							'corp' => 'int',
							'base' => 'string',
							'sells' => 'string',
							'prod_organics' => 'int',
							'prod_ore' => 'int',
							'prod_goods' => 'int',
							'prod_energy' => 'int',
							'prod_fighters' => 'int',
							'prod_torp' => 'int',
							'defeated' => 'string'
						),
						array(
							$playerinfo['sector'],
							$planetname, 
							0, 
							0, 
							0, 
							0, 
							0, 
							0, 
							0, 
							0, 
							$playerinfo['ship_id'], 
							0, 
							"N", 
							"N", 
							$default_prod_organics, 
							$default_prod_ore, 
							$default_prod_goods, 
							$default_prod_energy, 
							$default_prod_fighters, 
							$default_prod_torp, 
							"N"
						),
						''
					);
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET turns_used = turns_used + 1, 
							turns = turns - 1, 
							dev_genesis = dev_genesis - 1
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'ship_id' => $playerinfo['ship_id']
						)
					);
					echo $txt['bnt_gns_pcreate'];
				}
			}
		}
	    elseif ($playerinfo['team'] != $zoneinfo['owner'])
	    {
			echo $txt['bnt_gns_bforbid'];
	    }
		else
		{
			$smcFunc['db_insert']('insert',
				$dbtables['planets'],
				array(
					'sector_id' => 'int',
					'name' => 'string',
					'organics' => 'int', 
					'ore' => 'int', 
					'goods' => 'int',
					'energy' => 'int',
					'colonists' => 'int',
					'credits' => 'int',
					'fighters' => 'int',
					'torps' => 'int',
					'owner' => 'int',
					'corp' => 'int',
					'base' => 'string',
					'sells' => 'string',
					'prod_organics' => 'int',
					'prod_ore' => 'int',
					'prod_goods' => 'int',
					'prod_energy' => 'int',
					'prod_fighters' => 'int',
					'prod_torp' => 'int',
					'defeated' => 'string'
				),
				array(
					$playerinfo['sector'],
					$planetname, 
					0, 
					0, 
					0, 
					0, 
					0, 
					0, 
					0, 
					0, 
					$playerinfo['ship_id'], 
					0, 
					"N", 
					"N", 
					$default_prod_organics, 
					$default_prod_ore, 
					$default_prod_goods, 
					$default_prod_energy, 
					$default_prod_fighters, 
					$default_prod_torp, 
					"N"
				),
				''
			);
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET turns_used = turns_used + 1, 
					turns = turns - 1, 
					dev_genesis = dev_genesis - 1
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'ship_id' => $playerinfo['ship_id']
				)
			);
			echo $txt['bnt_gns_pcreate'];
		}
	}
	else
	{
		$smcFunc['db_insert']('insert',
			$dbtables['planets'],
			array(
				'sector_id' => 'int',
				'name' => 'string',
				'organics' => 'int', 
				'ore' => 'int', 
				'goods' => 'int',
				'energy' => 'int',
				'colonists' => 'int',
				'credits' => 'int',
				'fighters' => 'int',
				'torps' => 'int',
				'owner' => 'int',
				'corp' => 'int',
				'base' => 'string',
				'sells' => 'string',
				'prod_organics' => 'int',
				'prod_ore' => 'int',
				'prod_goods' => 'int',
				'prod_energy' => 'int',
				'prod_fighters' => 'int',
				'prod_torp' => 'int',
				'defeated' => 'string'
			),
			array(
				$playerinfo['sector'],
				$planetname, 
				0, 
				0, 
				0, 
				0, 
				0, 
				0, 
				0, 
				0, 
				$playerinfo['ship_id'], 
				0, 
				"N", 
				"N", 
				$default_prod_organics, 
				$default_prod_ore, 
				$default_prod_goods, 
				$default_prod_energy, 
				$default_prod_fighters, 
				$default_prod_torp, 
				"N"
			),
			''
		);
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET turns_used = turns_used + 1, 
				turns = turns - 1, 
				dev_genesis = dev_genesis - 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		echo $txt['bnt_gns_pcreate'];
	}
}

//-------------------------------------------------------------------------------------------------
$unlock = $smcFunc['db_query']('', 'UNLOCK TABLES');

echo '<br><br>';

TextMainMenu();

?>
